package com.msos.servlet.BManagerServlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.msos.model.All_Business.All_Business;
import com.msos.model.Message.Message;
import com.msos.model.RespPageBean.RespPageBean;
import com.msos.model.business.Business;
import com.msos.model.manager.Manager;
import com.msos.model.respBean.RespBean;
import com.msos.service.BManagerService.BManagerService;
import com.msos.utils.CommonsUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: BManagerServlet
 * @Description: TODO
 * @Author: hsx
 * @Date: 2022/4/15 19:33
 * @Version: v1.0
 */
@WebServlet(urlPatterns = "/bmanager")
public class BManagerServlet extends HttpServlet {
    BManagerService bManagerService = new BManagerService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if ("page".equals(action)){
            req.getRequestDispatcher("/WEB-INF/jsp/bManager/bManager.jsp").forward(req,resp);
        }else if ("data".equals(action)){
            String page = req.getParameter("page");
            String rows = req.getParameter("rows");
            String sort = req.getParameter("sort");
            String order = req.getParameter("order");
            RespPageBean respPageBean = bManagerService.getAllBusiness(page,rows,sort,order);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(new ObjectMapper().writeValueAsString(respPageBean));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if ("add".equals(action)){
            String bname = req.getParameter("bname");
            String bmoney = req.getParameter("bmoney");
            String bintro = req.getParameter("bintro");
            String bimg = req.getParameter("bimg");
            Integer result = bManagerService.addBusiness(bname,bintro,bimg,bmoney);
            RespBean respBean = null;
            if (result == CommonsUtils.EXIST){
                respBean = RespBean.error("业务已存在！");
            }else if (result == CommonsUtils.EXCEPTION_ERROR){
                respBean = RespBean.error("添加失败！");
            }else if (result == CommonsUtils.SUCCESS){
                respBean = RespBean.ok("添加成功！");
            }
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(new ObjectMapper().writeValueAsString(respBean));
        }else if ("open".equals(action)){
            String aid = req.getParameter("aid");
            Integer result = bManagerService.openBusinessByAid(aid);
            RespBean respBean = null;
            if (result == CommonsUtils.EXCEPTION_ERROR){
                respBean = RespBean.error("开通失败！");
            }else if (result == CommonsUtils.SUCCESS){
                respBean = RespBean.ok("开通成功！");
            }
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(new ObjectMapper().writeValueAsString(respBean));
        }else if ("close".equals(action)){
            String aid = req.getParameter("aid");
            Integer result = bManagerService.closeBusinessByAid(aid);
            RespBean respBean = null;
            if (result == CommonsUtils.EXCEPTION_ERROR){
                respBean = RespBean.error("关闭失败！");
            }else if (result == CommonsUtils.SUCCESS){
                respBean = RespBean.ok("关闭成功！");
            }
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(new ObjectMapper().writeValueAsString(respBean));
        }
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String aid = req.getParameter("aids");
        Integer result = bManagerService.deleteBusinessByAid(aid);
        RespBean respBean = null;
        if (result == CommonsUtils.NOTHING){
            respBean = RespBean.error("请选择业务进行删除！");
        }else if (result == CommonsUtils.SUCCESS){
            respBean = RespBean.ok("添加成功！");
        }else if (result == CommonsUtils.EXCEPTION_ERROR){
            respBean = RespBean.error("添加失败！");
        }
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(new ObjectMapper().writeValueAsString(respBean));
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper om = new ObjectMapper();
        All_Business all_business = om.readValue(req.getInputStream(), All_Business.class);
        Integer result = bManagerService.updateBusiness(all_business);
        RespBean respBean = null;
        if (result == CommonsUtils.EXIST){
            respBean = RespBean.error("业务已存在！");
        }else if (result == CommonsUtils.SUCCESS){
            respBean = RespBean.ok("修改成功！");
        }else if (result == CommonsUtils.EXCEPTION_ERROR){
            respBean = RespBean.error("修改失败！");
        }
        resp.setContentType("application/json;charset=utf-8");
        String json = om.writeValueAsString(respBean);
        resp.getWriter().write(json);
    }
}
